package com.warren.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Service
public class AsyncService {
    @Autowired
    MailService mailService;

    //异步方法
    @Async
    public void hello() {
        mailService.sendSimple();
        System.out.println("数据在处理");
    }

    public void asyncHello() {
        ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 5, 10, TimeUnit.SECONDS, workQueue);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("正在运行");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("结束");
            }
        });
    }
}
